package com.faithnmn.learnenglish.adapter;

import java.util.ArrayList;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.faithnmn.learnenglish.R;
import com.faithnmn.learnenglish.listeners.SharePostListener;
import com.faithnmn.learnenglish.model.Quote;
import com.faithnmn.learnenglish.util.CommonUtil;
import com.faithnmn.learnenglish.view.UnderlineTextView;

public class StickyListHeaderAdapter extends BaseAdapter implements
        StickyListHeadersAdapter, SectionIndexer {

    private Context mContext;
    private String[] titleHeaders;
    private LayoutInflater mInflater;
    private ArrayList<Quote> listQuote = new ArrayList<Quote>();
    private ArrayList<String> listHeaders = new ArrayList<String>();
    private SharePostListener sharePostListener;
    public StickyListHeaderAdapter(Context context,ArrayList<Quote> listQuote) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        assignVariables(listQuote);
    }

    public void setSharePostListener(SharePostListener sharePostListener){
    	this.sharePostListener = sharePostListener;
    }
    
    public String[] getTitleHeaders(){
    	return titleHeaders;
    }
    
	private void assignVariables(ArrayList<Quote> listQuote) {
		if(listQuote == null){
			return;
		}
		String[] lines = null;
		String value = null;
		listHeaders = new ArrayList<String>();
		for (Quote quote : listQuote) {
			value = "<b>"+CommonUtil.convertFBUnixtime(quote.created_time)+"</b>";
//			value += quote.message.substring(0, )
			lines = quote.message.split(System.getProperty("line.separator"));
			listHeaders.add(lines.length >=1?value+"<br>"+lines[0].replace("\n", ""):value);
		}
		lines = null;
		value = null;
        titleHeaders = listHeaders.toArray(new String[listHeaders.size()]);
        this.listQuote = listQuote;
	}

    public void setDatas(ArrayList<Quote> listQuote){
    	assignVariables(listQuote);
//    	notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listQuote.size();
    }

    @Override
    public Object getItem(int position) {
        return listQuote.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.test_list_item_layout, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.detail = (TextView)convertView.findViewById(R.id.detail);
            holder.share = (TextView)convertView.findViewById(R.id.share);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(listQuote.get(position).message);
        holder.detail.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String combinedLink = listQuote.get(position).id;
				String[] extractedLinks = combinedLink.split("_");
				String extractedLink = extractedLinks[1];
				String link = "https://www.facebook.com/permalink.php?story_fbid="+extractedLink+"&id=131304647058701";
				Intent detail = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
				mContext.startActivity(detail);
			}
		});
        holder.share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(sharePostListener != null){
					sharePostListener.onClickSharePost(position);
				}
			}
		});
        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        Log.e("getHeaderView","getHeaderView");
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.header, parent, false);
            holder.text = (UnderlineTextView) convertView.findViewById(R.id.text1);
            holder.passedTime = (TextView)convertView.findViewById(R.id.passedTime);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        holder.text.setText(Html.fromHtml(titleHeaders[position]));
        holder.passedTime.setText(CommonUtil.getPrettyTime(Long.parseLong(listQuote.get(position).created_time)*1000));
        return convertView;
    }

    /**
     * Remember that these have to be static, postion=1 should always return
     * the same Id that is.
     */
    @Override
    public long getHeaderId(int position) {
        return Long.parseLong(listQuote.get(position).created_time);
    }

    @Override
    public int getPositionForSection(int section) {
        return 1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 1;
    }

    @Override
    public Object[] getSections() {
        return titleHeaders;
    }

    private class HeaderViewHolder {
        private UnderlineTextView text;
        private TextView passedTime;
    }

    private class ViewHolder {
        private TextView text;
        private TextView detail,share;
    }

}
